Page({
  data: {
    idFrontUrl: '',  // 身份证正面照片
    idBackUrl: '',   // 身份证反面照片
    remoteUrl: getApp().globalData.remote_src
  },

  onLoad() {
  },

  // 上传身份证正面
  uploadIdFront() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        this.setData({
          idFrontUrl: tempFilePath
        });
        // 上传到服务器并调用OCR识别
        this.uploadToServer(tempFilePath, 'FRONT');
      }
    });
  },

  // 上传身份证反面
  uploadIdBack() {
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        const tempFilePath = res.tempFilePaths[0];
        this.setData({
          idBackUrl: tempFilePath
        });
        // 上传到服务器并调用OCR识别
        this.uploadToServer(tempFilePath, 'BACK');
      }
    });
  },

  // 上传图片到服务器
  uploadToServer(tempFilePath, cardSide) {
    const url = getApp().globalData.onlineService + '/api/infra/file';
    wx.uploadFile({
      url: url,
      filePath: tempFilePath,
      name: 'file',
      success: res => {
        const data = JSON.parse(res.data);
        console.log(data);
        if (data.code === 200) {
          // 成功处理
          const fileUrl = url + "?filename=" + data.filename;
          // 根据cardSide处理不同的逻辑
          if (cardSide === 'FRONT') {
            this.setData({
              idFrontUrl: fileUrl
            });
          } else {
            this.setData({
              idBackUrl: fileUrl
            });
          }
        } else {
          wx.showToast({
            title: data.msg || '上传失败',
            icon: 'none'
          });
        }
      },
      fail: err => {
        console.error(err);
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        });
      }
    });
  },

  // 提交表单
  onSubmit() {
    console.log(this.data.idFrontUrl, this.data.idBackUrl)
    if (!this.data.idFrontUrl || !this.data.idBackUrl) {
      wx.showToast({
        title: '请生成身份证照片',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '审核中...'
    });

    // 调用后端API提交认证信息
    const websiteUrl = getApp().globalData.onlineService;
    const token = getApp().globalData.userInfo.token;
    wx.request({
      url: `${websiteUrl}/api/auth/auth`,
      method: 'POST',
      header: {
        'Authorization': `Bearer ${token}`,
        'content-type': 'application/json'
      },
      data: {
        id_front_url: this.data.idFrontUrl,
        id_back_url: this.data.idBackUrl
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code === 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              setTimeout(() => {
                wx.navigateBack();
              }, 2000);
            }
          });
        } else {
          wx.showToast({
            title: res.data.msg || '审核失败',
            icon: 'error'
          });
        }
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '审核失败',
          icon: 'error'
        });
      }
    });
  }
});